<template>
<div>
  <el-tabs v-model="activeName" @tab-click="handleClick">

    <el-tab-pane label="编辑地图" name="first" >
      <div class="submitcontainer">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="编辑图层" prop="layerEditCode">
          <el-select v-model="ruleForm.layerEditCode" filterable clearable placeholder="请选择编辑图层" size="small">
            <el-option
                v-for="item in options"
                :key="item.layerCode"
                :label="item.layerName"
                :value="item.layerCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参考图层" prop="layerReferCode">
          <el-select v-model="ruleForm.layerReferCode" clearable filterable placeholder="请选择活参考图层" size="small">
            <el-option
                v-for="item in options"
                :key="item.layerCode"
                :label="item.layerName"
                :value="item.layerCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否审核" prop="isFlow">
          <el-radio-group v-model="ruleForm.isFlow">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="提醒方式" prop="type">-->
<!--          <el-checkbox-group v-model="ruleForm.type">-->
<!--            <el-checkbox label="短信" name="type"></el-checkbox>-->
<!--            <el-checkbox label="站内信" name="type"></el-checkbox>-->
<!--          </el-checkbox-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="提醒周">-->
<!--          <el-col :span="4" style="margin-right: 10px">-->
<!--            <el-input v-model="ruleForm.zhou" size="small" style="width: 100%!important;"></el-input>-->
<!--          </el-col>-->
<!--          <el-col :span="4">-->
<!--            <el-select v-model="ruleForm.layerEditCode" placeholder="周" size="small" style="width: 100%!important;">-->
<!--              <el-option label="周" value="shanghai"></el-option>-->
<!--              <el-option label="月" value="beijing"></el-option>-->
<!--            </el-select>-->
<!--          </el-col>-->
<!--        </el-form-item>-->
        <el-form-item class="submitbtn">
          <el-button @click="resetForm('ruleForm')" size="small">保存</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"  size="small">保存并发布</el-button>
        </el-form-item>
      </el-form>
    </div>
    </el-tab-pane>
    <el-tab-pane label="流程设定" name="second" :disabled="!detaile.flowTblId">
      <flow-panel  ref="modal" @changetable="changetable"></flow-panel>
    </el-tab-pane>
  </el-tabs>
</div>
</template>

<script>
import {api} from "../server/api";
import FlowPanel from "@/components/ef/panel";
export default {
  name: "EditMap",
  props:["ruleForm"],
  components: {
    FlowPanel
  },
  data() {
    return {
      activeName: 'first',
      options:[],
      detaile:{},
      oldetai:{},
      rules: {
        layerEditCode: [
          {required: true, message: '请选择编辑图层', trigger: 'change' }
        ],
      }
    };
  },
  mounted() {
    this.detaile=JSON.parse(JSON.stringify(this.ruleForm))
    this.oldetai=JSON.parse(JSON.stringify(this.ruleForm))
    console.log(this.detaile)
    console.log(this.ruleForm)
    this.layerList()
  },
  methods: {
    layerList(){
      api.layerList( { page: 1, limits: 200 }).then(res=>{
        const {records}=res
        this.options=records
        console.log(res)
      })
    },
    resetForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          api.farmOtherMapUpd(this.ruleForm).then(response => {
            this.$message.success("保存成功");
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    changetable(){
       this.activeName='first';
         this.getdatatie()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          api.farmOtherMapUpd(this.ruleForm).then(response => {
            const {launchStatus}=this.ruleForm
            if(launchStatus===0){
              api.farmOtherMapLaunch(this.ruleForm).then(res=>{
                this.$message.success("发布成功");
              })
            }else{
              this.$message.success("已发布");
            }
             this.getdatatie()
            if(this.ruleForm.mapType===0){
              this.updatcode()
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    updatcode(){
      const par={
        "oldLayer":this.oldetai.layerEditCode,   //旧的图层code
        "currLayer":this.ruleForm.isFlow===1?this.ruleForm.layerEditCode:'',  //新设置的图层code
      }
      api.updateLockAttr(par).then(res=>{
        console.log(res)
      })
    },
    getdatatie(){
 api.farmOtherMapDetail(this.ruleForm.mapId).then(res=>{
              this.detaile=res
            })
    },
    handleClick(tab) {
      if(parseInt(tab.index)===1){
        setTimeout(() => {
          this.$refs.modal.getMapdetail(this.detaile)
        }, 100);
      }else{
         this.getdatatie()
      }
      console.log(tab.index);
    }
  }
}
</script>

<style>
.el-tabs__nav-wrap::after{
  background-color: #d1d1d1;
  height: 1px;
}
.submitcontainer{
  width: 60%;
  margin-left: 20%;
  margin-top: 20px;
  height: 100%;
  position: relative;
}
.submitbtn{
 margin-top: 30%;
}
</style>